# encoding: utf-8

# 通用方法
module Capevaluation
  module Business
    module Prediction

      #验证输入的玩法和选项是否有效
      def check_play_items(play_code, items)
        #1. 从redis获取玩法配置, 验证玩法有效性
        play_info = check_play_code(play_code)

        #2. check 选项有效性
        play_info_map = JSON.parse play_info
        raise Capevaluation::Exception::InvalidPlayCode.new if play_info_map['items'].nil?
        items.to_s.split(',').each do |item|
          raise Capevaluation::Exception::InvalidPlayItem.new if play_info_map['items'].index(item).nil?
        end
      end

      #验证输入的玩法
      def check_play_code(play_code)
        #1. 从redis获取玩法配置, 验证玩法有效性
        keys = Capevaluation::Constant::WebService::REDIS_KEY_PLAY_CODE
        play_info = self.redis.hget(keys, play_code)
        raise Capevaluation::Exception::InvalidPlayCode.new if play_info.nil?
        return play_info
      end

      #得到主客队的队名
      def get_host_away_name(match_id)
        team_message = ""
        sql = "select host_id,away_id from football_match where match_id=#{match_id}"
        team_id = CaiQiu::Core::ServiceConn.DB_WBAI[sql]
        team_id.each do|item|
          host_id = item[:host_id]
          away_id = item[:away_id]
          host_sql = "select team_name_pre from football_team where team_id=#{host_id}"
          host_name_pre = CaiQiu::Core::ServiceConn.DB_WBAI[host_sql]
          host_name_pre.each do|item|
            host_name = item[:team_name_pre]
            team_message += "#{host_name} vs "
          end

          away_sql = "select team_name_pre from football_team where team_id=#{away_id}"
          away_name_pre = CaiQiu::Core::ServiceConn.DB_WBAI[away_sql]
          away_name_pre.each do|item|
            away_name = item[:team_name_pre]
            team_message += "#{away_name}"
          end
        end
        return team_message
      end



    end
  end
end

